package lgkj.meicqt20.ui;

import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import lgkj.meicqt20.R;
import lgkj.meicqt20.bean.MessageEvent;
import lgkj.meicqt20.utils.AllKindsOfConstant;
import lgkj.meicqt20.utils.HttpUtils;

/**
 * Created by Administrator on 2019/1/22 0022.
 */

public class NewsDetailsActivity extends BaseActivity {
    @BindView(R.id.beauty)
    ImageView beauty;
    @BindView(R.id.jin_xi)
    ImageView jinXi;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.webView)
    WebView webView;
    String webViewUrls;

    @Override
    public int setContentView() {
        return R.layout.activity_news_details;
    }

    @Override
    public void init() {
        HttpUtils.getQRCode(NewsDetailsActivity.this, ivCode);
        webView.setWebViewClient(new MyWebViewClient());
        webView.setWebChromeClient(new MyWebChromeClient());
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setTextZoom(200);
        webView.setInitialScale(25);
        webView.setBackgroundColor(0); // 设置背景色
    }

    public void up(int distancePx) {
        webView.scrollBy(0, (int) getResources().getDimension(R.dimen.down));
    }

    public void down(int distancePx) {
        webView.scrollBy(0, 200);
    }

    @Override
    public void initData() {
        String news_id = getIntent().getStringExtra("news_id");
        Map<String, String> params = new HashMap<>();
        if (news_id != null) {
            params.put("id", news_id);
        }
        params.put("news_type", "1");
        HttpUtils.getHttp(params, AllKindsOfConstant.getNewDetails, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                try {
                    webViewUrls = jsonObject.getJSONObject("data").getString("news_url");
                    webView.loadUrl(webViewUrls);
                } catch (JSONException e) {
                    Log.d(TAG, "successHttp: " + e.getMessage());
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        Log.d(TAG, "onResume");
        activitiesList.add(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        Log.i(TAG, "socket: " + messageEvent.toString());
        if ("control".equals(messageEvent.getTagName())) {
            String info = messageEvent.getMessage();
            fragmentDealOrder(info);
        }
    }

    private void fragmentDealOrder(String order) {
        switch (order) {
            case "up":
                up(0);
                break;
            case "down":
                down(0);
                break;
            case "back":
                finish();
                break;
        }
    }

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    private class MyWebChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            // TODO Auto-generated method stub
            super.onProgressChanged(view, newProgress);
            if (newProgress == 100) {
            }
        }
    }

}
